//节流函数
import { throttle } from 'miniprogram-licia'
Page({
  data: {
    visitorList: [],
    isEmpty: false
  },
  onLoad () {
    this.getVisitorList()
    this.getMoreVisitor = throttle(() => {
      if (!this.data.hasMore) return
      // 创建了节流函数
      this.getVisitorList(++this._current)
    }, 100)
  },
  async getVisitorList (current = 1, pageSize = 5) {
    const {
      code,
      data: { pageTotal, rows: visitorList }
    } = await wx.http.get('/visitor', { current, pageSize })
    if (code !== 10000) return wx.utils.toast()
    this.setData({ hasMore: current < pageTotal, visitorList: visitorList, isEmpty: visitorList.length === 0 })
    // 记录下来当前的页面
    this._current = current
  },
  goPassport () {
    wx.navigateTo({
      url: '/visitor_pkg/pages/passport/index'
    })
  }
})
